package com.campus.controller;

import com.campus.domain.dto.LoginFormDTO;
import com.campus.domain.dto.RegisterFormDTO;
import com.campus.domain.dto.UpdateUserDTO;
import com.campus.domain.vo.*;
import com.campus.service.IUserService;
import com.campus.utils.AliyunOssOperator;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;
    
    @Resource
    private AliyunOssOperator aliyunOssOperator;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<RegisterVO> register(@RequestBody RegisterFormDTO registerFormDTO){
        log.info("用户注册信息为：{}",registerFormDTO);
        RegisterVO registerVO = userService.register(registerFormDTO);
        return Result.ok(registerVO);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginFormDTO loginFormDTO){
        log.info("用户登录信息为：{}",loginFormDTO);
        String token = userService.login(loginFormDTO);
        return Result.ok(token);
    }

    /**
     * 用户登出
     */
    @GetMapping("/logout")
    public Result logout(){
        log.info("用户执行退出操作：");
        return Result.ok();
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result<UserInfoVO> getUserInfo(){
        log.info("获取登录的用户信息:");
        UserInfoVO userInfoVO = userService.getUserInfo();
        return Result.ok(userInfoVO);
    }
    
    /**
     * 获取用户详细信息
     */
    @GetMapping("/detail")
    public Result<UserDetailVO> getUserDetail(){
        log.info("获取用户详细信息:");
        UserDetailVO userDetailVO = userService.getUserDetail();
        return Result.ok(userDetailVO);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody UpdateUserDTO updateUserDTO){
        log.info("更新用户信息：{}", updateUserDTO);
        userService.updateUserInfo(updateUserDTO);
        return Result.ok("更新成功");
    }
    
    /**
     * 上传用户头像
     */
    @PostMapping("/upload/avatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) throws Exception {
        log.info("上传用户头像：{}", file.getOriginalFilename());
        
        // 调用阿里云OSS上传图片
        String url = aliyunOssOperator.upload(file.getBytes(), Objects.requireNonNull(file.getOriginalFilename()));
        
        // 返回图片URL
        Map<String, String> data = new HashMap<>();
        data.put("url", url);
        
        return Result.ok(data);
    }
    
    /**
     * 获取用户报名的活动列表
     */
    @GetMapping("/enrollments")
    public Result<List<UserEnrollmentVO>> getUserEnrollments() {
        log.info("获取用户报名的活动列表");
        List<UserEnrollmentVO> enrollments = userService.getUserEnrollments();
        return Result.ok(enrollments);
    }
    
    /**
     * 获取用户申领的失物列表
     */
    @GetMapping("/claims")
    public Result<List<UserClaimVO>> getUserClaims() {
        log.info("获取用户申领的失物列表");
        List<UserClaimVO> claims = userService.getUserClaims();
        return Result.ok(claims);
    }
}
